package util;

import com.lib.chat.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;


/**
 * 裁剪边框
 *
 * @author AC
 */
public class ClipCircleView extends View {

    /**
     * 边框距左右边界距离，用于调整边框长度
     */
    public static final int BORDERDISTANCE = 50;
//    public static final int BORDERDISTANCE = 0;

    private Paint mPaint;
    private Context mContext;

    public ClipCircleView(Context context) {
        this(context, null);
        mContext = context;
    }

    public ClipCircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
    }

    public ClipCircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        int innerCircle = dip2px(mContext, 140); // 背景框  210   头像框 270
        int ringWidth = height;  // 圆环宽度

        // 绘制内圆
        mPaint.setColor(getResources().getColor(R.color.dominanttone));
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(width / 2, height / 2, innerCircle, mPaint);

        // 绘制圆环
//        mPaint.setColor(0xaa000000);
//        mPaint.setStrokeWidth(ringWidth);
//        canvas.drawCircle(width / 2, height / 2, innerCircle + 1 + ringWidth
//                / 2, mPaint);

    }

    /* 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
